package cn.szzsi._sys;

import cn.szzsi.common.model.Navigation;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

import java.util.Date;
import java.util.List;

/**
 * 商户后台类 - 导航
 *
 */
public class NavigationController extends Controller{
    private Navigation navigation;
    private String shopId;

    // 列表
    public void list() {
        render("navigation_list.html");
    }
    //ajax list
    public void ajaxlist(){
        shopId="-1";
        List<Navigation> lpa = Navigation.dao.find("select * from  mall_navigation where uid = ? order by createDate desc",shopId);
        renderJson(lpa);
    }

    // 添加
    public void add() {
        shopId=getAttr("shopId");
        render("navigation_input.html");
    }

    // 编辑
    public void edit() {
        shopId="-1";
        String id = getPara("id", "");
        if (StrKit.notBlank(id)) {
            setAttr("navigation", Navigation.dao.findById(id));
        }
        render("navigation_input.html");
    }

    // 保存
    @Before(EvictInterceptor.class)
    @CacheName("BusinessNavigationList")
    public void save() {
        shopId=getAttr("shopId");
        navigation = getModel(Navigation.class);
        navigation.set("uid",shopId);
        String position = getPara("position");
        navigation.set("position", Navigation.Position.valueOf(position).ordinal());
        navigation.setCreateDate(new Date());
        navigation.save();
        renderJson(Ret.ok("info","添加成功").set("url","list"));
    }

    // 更新
    @Before(EvictInterceptor.class)
    @CacheName("BusinessNavigationList")
    public void update() {
        navigation = getModel(Navigation.class);
        String position = getPara("position");
        navigation.set("position", Navigation.Position.valueOf(position).ordinal());
        navigation.setModifyDate(new Date());
        navigation.update();
        renderJson(Ret.ok("info","修改成功").set("url","list"));
    }

    // 删除
    @Before(EvictInterceptor.class)
    @CacheName("BusinessNavigationList")
    public void delete() {
        String id=getPara("id");
        if(Navigation.dao.deleteById(id)){
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());

        }

    }
}
